//
//  Boutique_class_details_Controller.m
//  Food
//
//  Created by dllo on 15/7/15.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "Boutique_class_details_Controller.h"
#import "Boutique_class_details_cell.h"
#import "Boutique_class_details_model.h"
#import "BaseHeader.h"
#import "AFNetworkHandler.h"
#import "MJRefresh.h"
#import "HomePage_display_Controller.h"
static int i = 1;
@interface Boutique_class_details_Controller ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataArry;

@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation Boutique_class_details_Controller

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"naviBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.navigationItem.title = self.naviTitle;
    [self creatCollectionView];
    [self getdata];
    [self addFooter];
}

- (void) back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) viewWillAppear:(BOOL)animated
{
    i = 1;
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (void) creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth - 40) / 2.0, 120.0 / 375.0 *ScreenWidth);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumLineSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) collectionViewLayout:layout];
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[Boutique_class_details_cell class] forCellWithReuseIdentifier:@"cell"];
}



- (void) addFooter
{
    __unsafe_unretained typeof(self) vc = self;
    [self.collectionView addFooterWithCallback:^{
        i++;
        [self getdata:i];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc.collectionView reloadData];
            [vc.collectionView footerEndRefreshing];
        });
    }];
}

- (void) getdata:(int)i
{
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://121.40.54.251:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueVegetableList.do?is_traditional=0&page=%d&pageRecord=10&phonetype=0&user_id=0&typeId=%@", i ,self.classid];
    [AFNetworkHandler getWithAFNByURL:urlStr completion:^(id result) {
        NSArray *tempArr =result[@"data"];
        NSArray *tempArry = [Boutique_class_details_model arrayWithModelByArray:tempArr];
        NSLog(@"%@", tempArry);
        if (tempArry.count == 0) {
            self.collectionView.footerRefreshingText = @"数据加载完毕";
        }
        for (Boutique_class_details_model *model in tempArry) {
            [self.dataArry addObject:model];
        }
        [self.collectionView reloadData];
        [self.collectionView footerEndRefreshing];
        [activityIndicator stopAnimating];
    }];
}

- (void) getdata
{
    self.dataArry = [NSMutableArray array];
    NSString *urlStr = [NSString stringWithFormat:@"http://121.40.54.251:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueVegetableList.do?is_traditional=0&page=1&pageRecord=10&phonetype=0&user_id=0&typeId=%@", self.classid];
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    [AFNetworkHandler getWithAFNByURL:urlStr completion:^(id result) {
        NSArray *tempArry = result[@"data"];
        self.dataArry = [Boutique_class_details_model arrayWithModelByArray:tempArry];
        [self.collectionView reloadData];
        [activityIndicator stopAnimating];
    }];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Boutique_class_details_cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    Boutique_class_details_model *model = self.dataArry[indexPath.item];
    [cell setModel:model];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArry.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePage_display_Controller *display = [[HomePage_display_Controller alloc] init];
    Boutique_class_details_model *model = self.dataArry[indexPath.item];
    display.classid = model.vegetable_id;
    display.classTitle = model.name;
    display.MVBackGroundimage = model.imagePathLandscape;
    [self.navigationController pushViewController:display animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
